#include <iostream>
#include <string>
#include <fstream>

#include "contacts.pb.h"

const std::string path = "contacts.bin";

void add_people_info(contacts::people_info* pPeople)
{
    std::cout << "-------------新增联系人------------" << std::endl;
    std::cout << "联系人姓名: ";
    std::string str;
    std::getline(std::cin, str);
    pPeople->set_name(str);
    
    std::cout << "联系人年龄: ";
    int age = 0;
    std::cin >> age;
    pPeople->set_age(age);
    // 清除输入缓冲区
    // std::cin.clear();
    std::cin.ignore(256, '\n');
    
    for (size_t i = 1; true; ++i)
    {
        std::cout << "联系人电话" << i << " (回车键停止输入): ";
        std::getline(std::cin, str);
        if (str.empty())
            break;
        phone::phone_info* pPhone_info = pPeople->add_phone_numbers();
        pPhone_info->set_number(str);
        std::cout << "请输入该电话类型(1.移动电话/2.固定电话): ";
        int type = 0;
        std::cin >> type;
        std::cin.ignore(256, '\n');
        switch (type)
        {
        case 1:
            pPhone_info->set_type(phone::phone_info::phone_type::phone_info_phone_type_MOBILE_PHONE);
            break;
        case 2:
            pPhone_info->set_type(phone::phone_info::phone_type::phone_info_phone_type_TELEPHONE);
            break;
        default:
            break;
        }
    }
    contacts::Address address;
    std::cout << "请输入联系人家庭地址: ";
    std::getline(std::cin, str);
    address.set_home_address(str);
    std::cout << "请输入联系人单位地址: ";
    std::getline(std::cin, str);
    address.set_unit_address(str);
    // Address -> Any
    pPeople->mutable_data()->PackFrom(address);

    std::cout << "选择要添加的其他联系方式(1.qq/2.wechat): ";
    int other_contact;
    std::cin >> other_contact;
    std::cin.ignore(256, '\n');
    if (other_contact == 1)
    {
        std::cout << "请输入联系人qq号: ";
        std::getline(std::cin, str);
        pPeople->set_qq(str);
    }
    else if (other_contact == 2)
    {
        std::cout << "请输入联系人wechat号: ";
        std::getline(std::cin, str);
        pPeople->set_wechat(str);
    }
    else
        std::cout << "选择有误,未设置" << std::endl;

    for (size_t i = 0; true; ++i)
    {
        std::cout << "联系人备注" << i + 1 << " (回车键停止输入): " << std::endl;;
        std::cout << "标题: ";
        std::string key;
        std::getline(std::cin, key);
        if (key.empty())
            break;
        std::cout << "请输入备注: ";
        std::string value;
        std::getline(std::cin, value);
        pPeople->mutable_remark()->insert({key, value});
    }

    std::cout << "-----------新增联系人成功----------" << std::endl;
}

int main()
{
    contacts::contacts my_contacts;
    //读取本地已存在的联系人文件
    std::ifstream fin(path.c_str(), std::ifstream::in | std::ifstream::binary);
    if (!fin)
        std::cout << path << " not find, create new file" << std::endl;
    else
    {
        if (!fin.is_open())
            std::cout << path << " open err" << std::endl;
        
        if (!my_contacts.ParseFromIstream(&fin))
        {
            std::cout << "parse err" << std::endl;
            fin.close();
            return -1;
        }
        fin.close();
    }

    // 向通信录中添加联系
    add_people_info(my_contacts.add_peoples());

    // 将通讯录写入本地文件中
    std::ofstream fout(path.c_str(), std::ofstream::out | std::ofstream::trunc | std::ofstream::binary);
    if (!fout.is_open())
        std::cout << path << " open err" << std::endl;
    if (!my_contacts.SerializeToOstream(&fout))
        std::cout << "write err" << std::endl;
    std::cout << "write success" << std::endl;
    fout.close();
    return 0;
}